package edu.westga.cs3152.project2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SpellCheckerGUI2 extends javax.swing.JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private JMenuBar MenuBar;
	private JMenuItem openMenuItem;
	private JScrollPane scrollPane;
	private JPanel mainPanel;
	private JMenuItem checkSpellingMenuItem;
	private JMenu fileMenu;
	
	//The SpellChecker that will spell check the document
	SpellChecker spellChecker = new SpellChecker();
	//The SpellingSuggestor that will offer possible corrections to misspelled words
	SpellingSuggestor spellingSuggestor = new SpellingSuggestor();

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SpellCheckerGUI inst = new SpellCheckerGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public SpellCheckerGUI2() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			getContentPane().setLayout(new BorderLayout());
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				mainPanel = new JPanel();
				getContentPane().add(mainPanel);
				{
					scrollPane = new JScrollPane();
					mainPanel.add(scrollPane);
					scrollPane.setPreferredSize(new java.awt.Dimension(380, 228));
				}

				MenuBar = new JMenuBar();
				setJMenuBar(MenuBar);
				{
					fileMenu = new JMenu();
					MenuBar.add(fileMenu);
					fileMenu.setText("File");
					{
						openMenuItem = new JMenuItem();
						fileMenu.add(openMenuItem);
						openMenuItem.setText("Open");
						openMenuItem.addActionListener(this);
					}
					{
						checkSpellingMenuItem = new JMenuItem();
						fileMenu.add(checkSpellingMenuItem);
						checkSpellingMenuItem.setText("Check Spelling");
						checkSpellingMenuItem.addActionListener(this);
					}
				}
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		//Determines if the click was one of the buttons on the board.
		if (source.equals(this.openMenuItem)){
			try{
				//mainPanel.removeAll();
				JFileChooser fileChooser = new JFileChooser(".");
				int returnVal = fileChooser.showOpenDialog(null);
				String filePath = fileChooser.getSelectedFile().toString();
				if(filePath.substring(filePath.length() - 4, filePath.length()).equalsIgnoreCase(".txt")) {
					spellChecker.checkSpelling(fileChooser.getSelectedFile().toString());
				}
				else{
					JOptionPane.showMessageDialog(this, "The extension of the file must be \".txt\". Please open another file.");
				}
			} catch (Exception anyException) {
			   
			}
		}
		//Determines if the click was on the 5x5 menu item
		else if(source.equals(this.checkSpellingMenuItem)){
			try{
				String misspelled = "";
				ArrayList<String> misspelledWords = new ArrayList<String>(spellChecker.getMisspelledWords());
				if(!misspelledWords.toString().equalsIgnoreCase("[]")){
					Iterator<String> misspelledWordsIterator = misspelledWords.iterator();
					while(misspelledWordsIterator.hasNext()){
						String word = misspelledWordsIterator.next();
						JLabel misspelledWord = new JLabel();
						misspelledWord.setText(word + " | possible corrections: " + spellingSuggestor.getReplacementWords(word));
						//mainPanel.add(misspelledWord);
						//System.out.println(word + " | possible corrections: " + spellingSuggestor.getReplacementWords(word));
						misspelled = misspelled + word + " | possible corrections: " + spellingSuggestor.getReplacementWords(word) + "\n";
						scrollPane.add(misspelledWord);
					}
					
					mainPanel.updateUI();
				}
				else{
					JOptionPane.showMessageDialog(this, "No document has been opened. Please open a \".txt\" document.");
				}			
			}catch(Exception anyException){
				
			}
		}		
	}
}
